﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using FarseerGames.FarseerPhysics;

namespace LaughingDog.Managers
{
    public sealed class PhysicsManager : GameComponent
    {
        #region Private Members

        private PhysicsSimulator mSimulator;        

        #endregion

        #region Public Properties

        /// <summary>
        /// The Farseer PhysicsSimulator.
        /// </summary>
        public PhysicsSimulator Simulator { get { return mSimulator; } }
        
        #endregion

        #region Singleton

        static readonly PhysicsManager instance = new PhysicsManager(Engine.Game);

        // Explicit static constructor to tell C# compiler
        // not to mark type as beforefieldinit
        static PhysicsManager()
        {
        }

        PhysicsManager(Game game) 
            : base (game) 
        {
            mSimulator = new PhysicsSimulator(new Vector2(0, 100));
        }

        public static PhysicsManager Instance
        {
            get
            {
                return instance;
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Updates the PhysicsManager.
        /// </summary>
        /// <param name="gameTime">A snapshot of the current game time.</param>
        public override void Update(GameTime gameTime)
        {
            if(mSimulator.Enabled)
                mSimulator.Update(TimeManager.Instance.Delta);

            base.Update(gameTime);
        }

        /// <summary>
        /// Sets the gravity force used in the physics simulation.
        /// </summary>
        /// <param name="gravity">The force.</param>
        public void SetGravity(Vector2 gravity)
        {
            mSimulator.Gravity = gravity;                        
        }

        #endregion
    }
}

